package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
类简介：

功能：

时间：2020/4/3

*/
@Controller
@RequestMapping("ordersetting")
@ResponseBody
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){

        List<String[]> strings;

        try {
            strings = POIUtils.readExcel(excelFile);
        } catch (IOException e) {
            return new Result(false, "读取文件失败",e.getMessage());
        }

        orderSettingService.upload(strings);

        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        ArrayList<Map<String, Object>> orderSettings;
        try {
            orderSettings = orderSettingService.findAll(date);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL,e.getMessage());
        }

        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orderSettings);

    }

    @RequestMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.edit(orderSetting.getNumber(),orderSetting.getOrderDate());
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL,e.getMessage());
        }

        return new Result(true,MessageConstant.ORDERSETTING_FAIL);

    }

}
